using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using System.IO;
using Drawing = System.Drawing;
using Imaging = System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Scurvy.Media.Avi;

namespace Scurvy.Media.Pipeline
{
    /// <summary>
    /// Imports an AVI video
    /// </summary>
    [ContentImporter(".avi",
        CacheImportedData=false, 
        DefaultProcessor="VideoStreamProcessor", 
        DisplayName="Avi Video - Scurvy Media")]
    public class AviImporterBgr565 : VideoImporter<Microsoft.Xna.Framework.Graphics.PackedVector.Bgr565>
    {
        #region Fields

        private AviManager _aviManager;
        private AviVideoStream _stream;
        private int _frameCount;
        private double _frameRate;

        #endregion

        protected override void Initialize(string filename)
        {
            
            _aviManager = new AviManager(filename, true);
            _stream = _aviManager.GetVideoStream();
            _stream.GetFrameOpen();
            _frameCount = _stream.CountFrames;
            _frameRate = _stream.FrameRate;

            System.Diagnostics.Debug.WriteLine(string.Format("Importing {0} frames with a framerate of {1}", _frameCount, _frameRate));
        }
        
        protected override int NumberOfFrames
        {
            get { return _frameCount; }
        }

        protected override double FrameRate
        {
            get { return _frameRate; }
        }


        protected override System.Drawing.Bitmap GetFrame(int i)
        {
            System.Drawing.Bitmap bmp = _stream.GetBitmap(i, Imaging.PixelFormat.Format16bppRgb565);
            System.Diagnostics.Debug.WriteLine(string.Format("\tGetting Frame #{0}", i));
            return bmp;
        }
    }
}
